<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2020 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use think\facade\View;
use think\exception\ValidateException;
use app\admin\BaseController;
use app\admin\model\AdminMenu as MenuModel;
use app\admin\validate\AdminMenu as MenuValidate;
/**
 * 管理员菜单管理
 */
class AdminMenu extends BaseController
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        if ($this->request->isPost()) {
            return json([
                'status'     => 'success', 
                'message'    => '请求成功', 
                'data'       => $this->request->systemMenu, 
                'publicMenu' => $this->request->publicMenu,
            ]);
        } else {
            return View::fetch();
        }
    }

    /**
     * 保存新建的资源
     */
    public function save()
    {
        try {
            $input = input();
            validate(MenuValidate::class)->scene('save')->check($input);
            $p = MenuModel::create([
                'pid'        => $input['pid'],
                'title'      => $input['title'],
                'icon'       => $input['icon'],
                'path'       => $input['path'],
                'sort'       => $input['sort'],
                'status'     => $input['status'],
                'ifshow'     => $input['ifshow'],
                'logwriting' => $input['logwriting'],
            ]);
            if (isset($input['isList']) && $input['isList'] === 1) {
                MenuModel::create([
                    'pid'        => $p['id'],
                    'title'      => $input['title'],
                    'icon'       => $input['icon'],
                    'path'       => $input['path'],
                    'sort'       => 0,
                    'status'     => 1,
                    'ifshow'     => 1,
                    'logwriting' => $input['logwriting'],
                ]);
            }
            return json(['status' => 'success', 'message' => '新增成功']);
        } catch ( ValidateException $e ) {
            return json($e->getError());
        }
    }

    /**
     * 保存更新的资源
     */
    public function update()
    {
        try {
            $input = input();
            $scene = isset($input['field']) ? 'field' : '';
            validate(MenuValidate::class)->scene($scene)->check($input);
            $save  = MenuModel::find($input['id']);
            if (isset($input['field'])) {
                $field = $input['field'];
                $save->$field = $input['value'];
            } else {
                $save->pid        = $input['pid'];
                $save->title      = $input['title'];
                $save->icon       = $input['icon'];
                $save->path       = $input['path'];
                $save->sort       = $input['sort'];
                $save->status     = $input['status'];
                $save->ifshow     = $input['ifshow'];
                $save->logwriting = $input['logwriting'];
            }
            $save->save();
            return json(['status' => 'success', 'message' => '修改成功']);
        } catch ( ValidateException $e ) {
            return json($e->getError());
        }
    }

    /**
     * 删除指定资源
     */
    public function delete()
    {
        try {
            $input = input();
            validate(MenuValidate::class)->scene('delete')->check($input);
            MenuModel::recursiveDestroy($input['ids']);
            return json(['status' => 'success', 'message' => '删除成功']);
        } catch ( ValidateException $e ) {
            return json($e->getError());
        }
    }
}
